

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;

public class HbaseClientDemo {

    Connection conn = null;
    Table table = null;
@Before
public void getConn() throws IOException{
    //构建一个链接对象
    Configuration conf = HBaseConfiguration.create();//会自动加载hbase-site.xml
    conf.set("hbase.zookeeper.quorum","master:2181,slave1:2181,slave2:2181");
    conn = ConnectionFactory.createConnection(conf);
    table = conn.getTable(TableName.valueOf("t_user"));
}
/**
 * SQL语言共分为四大类：数据查询语言DQL，数据操纵语言DML，数据定义语言DDL，数据控制语言DCL
 * 1) DQL基本结构是由SELECT子句，FROM子句，WHERE子句组成的查询块
 * 2) DML主要有三种形式：插入：INSERT  更新：UPDATE  删除：DELETE
 * 3) DDL用来创建数据库中的各种对象-----表、视图、索引、同义词、聚簇等
 * 4) 数据控制语言DCL用来授予或回收访问数据库的某种特权，并控制数据库操纵事务发生的时间及效果，对数据库实行监视等。
 * <p>
 * <p>
 * DDL
 *
 *
 */
@Test
    public void teatCreateTable() throws IOException{
    //从连接中构造一个DDL操作器
    Admin admin = conn.getAdmin();
    //创建一个表定义描述对象
    HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("t_user"));
    //创建列族定义描述对象
    HColumnDescriptor hColumnDescriptor_1 = new HColumnDescriptor("base_info");
    HColumnDescriptor hColumnDescriptor_2 = new HColumnDescriptor("extra_info");
    //设置该列族中存储数据的最大版本数，默认是1
    hColumnDescriptor_1.setMaxVersions(3);
    //将列族定义信息对象放入表定义对象中
    hTableDescriptor.addFamily(hColumnDescriptor_1);
    hTableDescriptor.addFamily(hColumnDescriptor_2);

    //用DDL 操作器对象：admin来建表
    admin.createTable(hTableDescriptor);
    //关闭连接
    admin.close();
    conn.close();

}

/**
 * 删除表
 */

@Test
    public void testDropTable() throws IOException{
    Admin admin = conn.getAdmin();
    //停用表
    admin.disableTable(TableName.valueOf("t_user"));
    //删除表
    admin.deleteTable(TableName.valueOf("t_user"));
    admin.close();
    conn.close();
}
/**
 * DML 数据的增删改查
 * <p>
 *   添加一条数据
 */
@Test
    public void insertData() throws IOException{
    Put put = new Put(Bytes.toBytes("zhangsan"));
    put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
    put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("22"));
    table.put(put);
    System.out.println("添加成功");
}
/**
 *批添加数据
 *
 */
@Test
    public void insertBatch() throws IOException{
    ArrayList<Put> arrayList = new ArrayList<Put>();
    for (int i = 45; i < 50; i++) {
        Put put = new Put(Bytes.toBytes("1234" + i));
        put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu" + i));
        put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("password"), Bytes.toBytes("1234" + i));
        arrayList.add(put);

}
table.put(arrayList);

}
/**
 * 修改数据 相同rowkey 列簇 column 会覆盖新值
 */
@Test
    public void updateData() throws IOException{
    Put put = new Put(Bytes.toBytes("zhangsan"));
    put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan2"));
    put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("23"));
    table.put(put);
}
/**
 * 删除数据
 *
 */
@Test
    public void deleteData()throws IOException{
    //按行健删除
    Delete delete = new Delete(Bytes.toBytes("zhangsan"));
    table.delete(delete);

    //按列族删除
    Delete delete1 = new Delete(Bytes.toBytes("123445"));
    delete1.addFamily(Bytes.toBytes("extra_info"));
    table.delete(delete1);

    //按cell删除
    Delete delete2 = new Delete(Bytes.toBytes("123447"));
    delete2.addColumns(Bytes.toBytes("extra_info"), Bytes.toBytes("name"));
    table.delete(delete2);
    System.out.println("删除数据成功");
}
/**
 * 查询数据
 *
 */
@Test
public void queryData() throws IOException {
    Get get = new Get(Bytes.toBytes("123449"));
    Result result = table.get(get);
    System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("extra_info"), Bytes.toBytes("password"))));
    System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("extra_info"), Bytes.toBytes("name"))));
}
@Test
public void scanData() throws IOException {
    Scan scan = new Scan();
    // scan.addFamily(Bytes.toBytes("extra_info")); //扫描指定family

    // 扫描cell指定
    // scan.addColumn(Bytes.toBytes("extra_info"),
    // Bytes.toBytes("password"));

    // 按rowkey字典序扫描
    // scan.setStartRow(Bytes.toBytes("wangsf_0"));
    // scan.setStopRow(Bytes.toBytes("wangwu"))

    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("extra_info"), Bytes.toBytes("password"))));
        System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("extra_info"), Bytes.toBytes("name"))));
    }

}

}